package com.gzy.wbapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gzy.wbapplication.events.LoginCompleteEvent;
import com.gzy.wbapplication.manager.AuthManager;
import com.gzy.wbapplication.model.Result;
import com.gzy.wbapplication.model.User;
import com.gzy.wbapplication.model.para.LoginPara;
import com.gzy.wbapplication.net.AuthApi;
import com.gzy.wbapplication.net.URLs;
import com.gzy.wbapplication.net.core.XXModelCallback;
import com.gzy.wbapplication.net.core.XXURLUtils;
import com.gzy.wbapplication.utils.SPUtils;
import com.kaopiz.kprogresshud.KProgressHUD;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.nio.charset.Charset;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;

public class LoginActivity extends AppCompatActivity {

    EditText edt_username;
    EditText edt_password;

    KProgressHUD hud;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        findViewById(R.id.back).setOnClickListener((v) -> {
            finish();
        });
        findViewById(R.id.btn_login).setOnClickListener((v) -> {
            login();
        });
        findViewById(R.id.tv_jump2signup).setOnClickListener((v) -> {
            startActivity(new Intent(this, SignupActivity.class));
        });

        edt_username = findViewById(R.id.edt_username);
        edt_password = findViewById(R.id.edt_password);

        EventBus.getDefault().register(this);
    }

    void login() {

        String username = edt_username.getText().toString();
        String password = edt_password.getText().toString();

        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }


        hud = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
       hud.show();

        AuthApi.doLogin(username, password, new XXModelCallback<Result<String>>(new TypeReference<Result<String>>() {
        }) {
            @Override
            public void onResponseData(Call call, Result<String> model) {
                // 登录成功 获取到 token
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        try {
//                            Thread.sleep(1000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        runOnUiThread(()->{
//
//                        });
//
//                    }
//                }).start();

                loadUserinfo(model.getData());
            }

            @Override
            public void onFailure2(Call call, IOException e, ErrType type, String message) {
                hud.dismiss();
                Toast.makeText(LoginActivity.this, "" + message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void loadUserinfo(String token) {
        AuthApi.getUserinfo(token, new XXModelCallback<Result<User>>(new TypeReference<Result<User>>() {
        }) {
            @Override
            public void onResponseData(Call call, Result<User> model) {
                // token;
                // model.getData();
                hud.dismiss();

                Toast.makeText(LoginActivity.this, "" + model.getData().getUsername(), Toast.LENGTH_SHORT).show();
                // 登录流程就完成
                AuthManager.login(getApplicationContext(), token, model.getData());
                // 保存 数据完成

                // 跳转到主界面

                startActivity(new Intent(LoginActivity.this, MainActivity.class));

                EventBus.getDefault().post(new LoginCompleteEvent());
            }

            @Override
            public void onFailure2(Call call, IOException e, ErrType type, String message) {
                hud.dismiss();
                Toast.makeText(LoginActivity.this, "" + message, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMessage(LoginCompleteEvent message) {
        finish();
    }

}
